package com.dhecp.project.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.DateUtils;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysModuleTask;
import com.dhecp.project.system.mapper.SysModuleTaskMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysModuleNoticeService;
import com.dhecp.project.system.service.SysModuleTaskService;

/**
 * 功能任务 服务实现层
 * @author DH-Xxj
 * @version 2020年4月24日
 */
@Service
public class SysModuleTaskServiceImpl implements SysModuleTaskService {

	@Autowired
	private SysModuleTaskMapper moduleTaskMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private SysModuleNoticeService moduleNoticeService;
	
	/**
	 * 查询未处理任务数
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public Integer queryModuleTaskWcls(Map<String, Object> params) {
		params.put("cUser", ShiroUtils.getMandatorId());
		return moduleTaskMapper.selectWcls(params);
	}
	
	/**
	 * 查询计划数量
	 * @param params 无必要参数
	 * @return
	 * @author DH-lwl
	 */
	@Override
	public Integer queryModuleTaskPlans(Map<String, Object> params) {
		params.put("assignee", ShiroUtils.getMandatorId());
		return moduleTaskMapper.selectPlans(params);
	}
	
	/**
	 * 查询总结
	 * @param params 无必要参数
	 * @return
	 * @author DH-lwl
	 */
	@Override
	public Integer queryModuleTaskFinishs(Map<String, Object> params) {
		params.put("assignee", ShiroUtils.getMandatorId());
		return moduleTaskMapper.selectFinishs(params);
	}

	/**
	 * 查询未处理任务
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysModuleTask> queryModuleTaskWcl(Map<String, Object> params) throws Exception {
		params.put("cUser", ShiroUtils.getMandatorId());
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		return moduleTaskMapper.selectWcl(params);
	}
	
	/**
	 * 查询已处理任务
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysModuleTask> queryModuleTaskYcl(Map<String, Object> params) throws Exception {
		params.put("cUser", ShiroUtils.getMandatorId());
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		return moduleTaskMapper.selectYcl(params);
	}
	
	/**
	 * 查询当前用户为发送人数据
	 * @param params 无必要参数
	 * @return
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysModuleTask> queryModuleTaskFs(Map<String, Object> params) throws Exception {
		params.put("bUser", ShiroUtils.getUserId());
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		return moduleTaskMapper.selectFs(params);
	}
	
	/**
     * 新增功能任务
     * 
     * @param 
     */
    @Override
	public AjaxResult insertModuleTask(Map<String, Object> params){
    
    	if (params.get("moduleTable") == null || params.get("moduleTable") == "") {
    		return AjaxResult.error("请提供必要的参数[功能唯一标识]：ModuleTable");
		}else{
			SysModuleTask moduleTask = new SysModuleTask();
			
			moduleTask.setbUser(ShiroUtils.getMandatorId());
			if(params.get("moduleTable")!=null && params.get("moduleTable")!="")moduleTask.setModuleTable(params.get("moduleTable").toString());
			if(params.get("cUser")!=null && params.get("cUser")!="")moduleTask.setcUser(params.get("cUser").toString());
	        if(params.get("pGuid")!=null && params.get("pGuid")!="")moduleTask.setpGuid(params.get("pGuid").toString());
			if(params.get("taskContent")!=null && params.get("taskContent")!="")moduleTask.setTaskContent(params.get("taskContent").toString());
			if(params.get("taskAbstract")!=null && params.get("taskAbstract")!="")moduleTask.setTaskAbstract(params.get("taskAbstract").toString());
			if(params.get("taskState")!=null && params.get("taskState")!="")moduleTask.setTaskState(params.get("taskState").toString());
			if(params.get("pageTitle")!=null && params.get("pageTitle")!="")moduleTask.setPageTitle(params.get("pageTitle").toString());
			if(params.get("pageURL")!=null && params.get("pageURL")!="")moduleTask.setPageURL(params.get("pageURL").toString());
			if(params.get("dataURL")!=null && params.get("dataURL")!="")moduleTask.setDataURL(params.get("dataURL").toString());
			moduleTask.setCreateTime(new Date());
			if(params.get("limitTime")!=null && params.get("limitTime")!="")moduleTask.setLimitTime(DateUtils.parseDate(params.get("limitTime")));
			
			moduleTaskMapper.insertTask(moduleTask);
			return AjaxResult.success("操作成功");
    	}
    }
    
    /**
	 * 修改功能任务状态
	 * @param guid 必填参数
	 * @param taskState 必填参数：保存时可省略，提交时为（taskTaskState = "已处理"）
	 * @param 
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
    public AjaxResult updateTaskState(Map<String, Object> params) {
		if (params.get("guid") == null || params.get("guid") == ""){
			return AjaxResult.error("缺少必要参数：guid【功能任务guid】。");
		}
		if (params.get("taskState") == null || params.get("taskState") == ""){
			return AjaxResult.error("缺少必要参数：taskState【状态】。");
		}
        String taskTaskState = "";
        if (params.get("taskTaskState") != null && "已处理".equals(params.get("taskTaskState").toString())){
        	taskTaskState = "已处理";
        }
        if ("已处理".equals(taskTaskState)){
        	SysModuleTask moduleTask = new SysModuleTask();
        	moduleTask.setGuid(params.get("guid").toString());
        	moduleTask.setTaskState(taskTaskState);
        	moduleTask.setDealTime(new Date());
        	moduleTaskMapper.updateTaskState(moduleTask);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
	
	/**
	 * 修改功能任务状态并生成通知
	 * @param guid 必填参数
	 * @param taskState 必填参数：保存时可省略，提交时为（taskTaskState = "已处理"）
	 * @param moduleTable 功能唯一标识
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
    public AjaxResult reply(Map<String, Object> params) {
		AjaxResult ajaxResult = new AjaxResult();
		ajaxResult=this.updateTaskState(params);
		if("0".equals(ajaxResult.get("code").toString())){
			ajaxResult=moduleNoticeService.insertModuleNotice(params);
		}
		return ajaxResult;
    }
    
}
